package com.winit.psc.manager;

import com.winit.menu.model.MatrixMenu;
import com.winit.psc.model.MatrixPsc;
import com.winit.psc.repository.IPscRepositoryExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangjian on 2017/12/1.
 */
@Component
public class PscManager {
    private static Logger log = LoggerFactory.getLogger(PscManager.class);

    @Autowired
    private IPscRepositoryExt pscRepositoryExt;

    //根据条件查询psc列表
    public List<MatrixPsc> getList(String id, String type, List<String> codes, String name){
        List<MatrixPsc> list = new ArrayList<>();
        MatrixPsc searchConditions = new MatrixPsc();
        searchConditions.setId(id);
        searchConditions.setType(type);
        searchConditions.setName(name);
        if (CollectionUtils.isEmpty(codes)){
            list = this.pscRepositoryExt.searchByPropertiesNotNull(searchConditions);
        }else {
            List<MatrixPsc> tempList = null;
            for (String code : codes){
                searchConditions.setCode(code);
                tempList = this.pscRepositoryExt.searchByPropertiesNotNull(searchConditions);
                if (!CollectionUtils.isEmpty(tempList)){
                    list.addAll(tempList);
                }
            }
        }
        return list;
    }
    public List<MatrixPsc> tree(String id, String type, String code, String name, String parentId, Integer withChildren){
        if (StringUtils.isEmpty(id) && StringUtils.isEmpty(code) && StringUtils.isEmpty(name) && StringUtils.isEmpty(parentId)){
            parentId = "0";
        }
        return this.getPsc(id, type, code, name, parentId, withChildren);
    }
    public List<MatrixPsc> getPsc(String id, String type, String code, String name, String parentId, Integer withChildren){
        MatrixPsc searchConditions = new MatrixPsc();
        searchConditions.setId(id);
        searchConditions.setType(type);
        searchConditions.setName(name);
        searchConditions.setCode(code);
        searchConditions.setParentId(parentId);
        List<MatrixPsc> roots = this.pscRepositoryExt.searchByPropertiesNotNull(searchConditions);
        if (CollectionUtils.isEmpty(roots)){
            return roots;
        }
        if (withChildren.intValue() == 1) {
            for (MatrixPsc matrixPsc : roots) {
                this.dealChildren(matrixPsc, true);
        }
        }
        return roots;
    }


    /**
     *
     * @param matrixPsc
     * @param round
     * @return
     */
    private List<MatrixPsc> dealChildren(MatrixPsc matrixPsc, boolean round){
        List<MatrixPsc> children = null;
        String parentId = matrixPsc.getId();
        List<String> childCodes = matrixPsc.getChilds();
        if (CollectionUtils.isEmpty(childCodes)){   //兼容child为空的情况
            MatrixPsc searchConditions = new MatrixPsc();
            searchConditions.setParentId(parentId);
            children = this.pscRepositoryExt.searchByPropertiesNotNull(searchConditions);
            matrixPsc.setChildren(children);
            if (CollectionUtils.isEmpty(children)){
                return children;
            }
            if (!round){    //若每个MatrixPsc需要特别处理，该条件下移至for循环内
                return children;
            }
            for (MatrixPsc child : children) {
                this.dealChildren(child, round);
            }
            return children;
        }
        MatrixPsc searchConditions = new MatrixPsc();
        searchConditions.setParentId(parentId);
        for (String childCode : childCodes){
            searchConditions.setCode(childCode);
            List<MatrixPsc> tempChild = this.pscRepositoryExt.searchByPropertiesNotNull(searchConditions);
            if (CollectionUtils.isEmpty(tempChild)){
                continue;
            }
            MatrixPsc child = tempChild.get(0);
            if (children == null){
                children = new ArrayList<>();
            }
            if (round){
                this.dealChildren(child, round);
            }
            children.add(child);
        }
        matrixPsc.setChildren(children);
        return children;
    }


}
